package org.hxzon.configdesigner.web.components;

import java.util.UUID;

import org.apache.tapestry5.EventConstants;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Parameter;
import org.apache.tapestry5.annotations.Property;
import org.hxzon.configdesigner.core.CfgInfo;
import org.hxzon.configdesigner.core.CfgParser;
import org.hxzon.configdesigner.core.CfgValue;

public class AddFieldForm {
    @Parameter
    @Property
    private CfgValue cfg;

    @Property
    private String newFieldId;
    @Property
    private int newFieldType;
    @Property
    private String newFieldLabel;
    @Property
    private String newFieldValue;//值或者默认值（如果是列表或映射表的元素）

    @OnEvent(EventConstants.SUBMIT)
    Object addField() {
        CfgInfo cfgInfo = new CfgInfo();
        cfgInfo.setId(newFieldId);
        cfgInfo.setLabel(newFieldLabel);
        cfgInfo.setType(newFieldType);
        cfgInfo.setDefaultValue(CfgParser.converValue(newFieldType, newFieldValue));
        if (cfg.isElement()) {

        } else {
            CfgValue newCfg = new CfgValue(cfgInfo, UUID.randomUUID());
            newCfg.setValue(cfgInfo.getDefaultValue());
            cfg.getCfgInfo().getParts().add(cfgInfo);
            cfg.addValue(newCfg);
        }
        return null;
    }
}
